<?php
/**
 * 用户角色信息
 */
apf_require_class("Bll_BLL");
class Bll_Attendance_Role extends BLL {

    /**
     * 获得角色对应的APP
     *
     * @param unknown_type $code
     * @return array
     */
    public function get_role_app($role_id) {
        $this->session_act();
        $result = array();
        $role_id = (int)$role_id;
        if(empty($role_id)) {
            return $result;
        }
        $rs = $this->get_role_dao()->find_by_id($role_id);
        apf_require_class("Bll_Attendance_UserInfo");
        $user_info_bll =  new Bll_Attendance_UserInfo;
        $is_bos_role = $user_info_bll->has_subordinate($_SESSION['user_id']);



        if(! empty($rs)) {
            $app_ids = explode(',', $rs['app_ids']);

            //是否有下属，有下属的添加bos的审批权限
            if($is_bos_role){
                $app_ids[] = 5;//5表示审批权限
                $app_ids[] = 6;//6表示考勤报告
            }
            $result = $this->get_app_dao()->get_apps($app_ids);
        }

        return $result;
    }

    /**
     * 获得管理员角色菜单
     * @@return array
     */
    public function get_admin_role() {
        $result = array();
        $rs = $this->get_role_dao()->get_admin_role();
        if(! empty($rs)) {
            $app_ids = explode(',', $rs['0']['app_ids']);
            $result = $this->get_app_dao()->get_apps($app_ids);
        }
        return $result;
    }

    /**
     * 获得所有角色
     *
     * @return array
     */
    public function get_all_role() {
        $result = $this->get_role_dao()->find_assoc(array(), 'id desc');
        return $result;
    }


    // 获取主管id,org_relation 表
    /**Jerry
     * 支持取单个上级 传参 int level 以及多个上级 传参 array level
     * @retun user_id or array('level'=>user_id)
     */
    public function get_sup_id($user_id,$level = 0) {
        $this->session_act();
        $rows = $this->get_orgrelation_dao()->find(array(
                'user_id' => $user_id
        ));
        if(empty($rows)) {
            return false;
        }
        $rs = array();
        foreach($rows as $v) {
            $data[$v['level']] = $v['superior_id'];
        }
        if(is_int($level) && $level >0){
            $rs = $data[$level];
        }elseif(is_array($level) && !empty($level)){
            foreach ($level as $val){
                if($data[$val]){
                    $rs[$val] = $data[$val];
                }
            }
        }else{
            $rs = $data;
        }
        return $rs;
    }

    // 获取主管姓名
    /**Jerry
     * 支持取单个上级 传参 int ids 以及多个上级 传参 array ids
    * @retun array ($level => $name)
    */
    public function get_sup_name($ids){
        if(is_string($ids) && $ids >0){
            $data = $this->get_userinfo_dao()->find_by_id($ids);
            $rs = $data['name'];
        }elseif(is_array($ids) && !empty($ids)){
            $data = $this->get_userinfo_dao()->find_by_ids($ids);
            foreach($ids as $level => $user_id){
                $rs[$level] = $data[$user_id]['name'];
                ksort($rs);
            }
        }else{
            return false;
        }
        return $rs;
    }

    /**
     * 得到下属的信息
     * @param unknown_type $user_id
     */
    public function get_subordinate_by_user($user_id,$level = 1,$order = 'user_id asc') {
        $result = array();
        if(empty($user_id)) {
            return $result;
        }

        $where = array('superior_id'=>$user_id);
        if(!empty($level)) {
            $where['level'] = $level;
        }
        $result = $this->get_orgrelation_dao()->find_assoc($where,$order);
        return $result;
    }

    /**
     * @return DAO_Attendance_AdminRole
     */
    private function get_role_dao() {
        $dao = $this->get_dao('DAO_Attendance_AdminRole');
        return $dao;
    }

    /**
     *
     * @return Dao_Attendance_Apps
     */
    private function get_app_dao() {
        $dao = $this->get_dao('Dao_Attendance_Apps');
        return $dao;
    }

    /**
     *
     * @return DAO_Attendance_LeaveRecord
     */
    private function get_leaverecord_dao() {
        $dao = $this->get_dao('DAO_Attendance_LeaveRecord');
        return $dao;
    }

    /**
     *
     * @return Dao_Attendance_OrgRelation
     */
    private function get_orgrelation_dao() {
        $dao = $this->get_dao('Dao_Attendance_OrgRelation');
        return $dao;
    }

    /**
     * return DAO_Attendance_UserRole
     */
    private function get_user_role_dao(){
        return $this->get_dao('DAO_Attendance_UserRole');
    }

    /**
     * return DAO_Attendance_UserInfo
     */
    private function get_userinfo_dao(){
        return $this->get_dao('DAO_Attendance_UserInfo');
    }
}
